package com.lanou.beautifulwardrobe.home.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ShowProducts;

import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class HomeWaterItemAdapter extends RecyclerView.Adapter<HomeWaterItemAdapter.WaterItemViewHolder> {

    private List<ShowProducts> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ImageLoader imageLoader;

    public void addData(List<ShowProducts> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public WaterItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.showproducts, null);
        return new WaterItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final WaterItemViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            ShowProducts products = datas.get(position);
            holder.showProductPriceTv.setText(products.getTaobao_selling_price());
            Log.i("lanou",products.getTaobao_selling_price());
            getBitMap(products.getTaobao_pic_url(),holder.showProductIv);

        }

    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    class WaterItemViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView showProductIv;
        private TextView showProductPriceTv;

        public WaterItemViewHolder(View view) {
            super(view);
            showProductIv = (NetworkImageView) view.findViewById(R.id.showProductIv);
            showProductPriceTv = (TextView) view.findViewById(R.id.showProductPriceTv);

        }
    }
    /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }
}
